/**
 * Alibaba.com.cn Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.starlink.common.strategy.paynotify;

import cn.hutool.core.util.NumberUtil;
import com.starlink.common.domain.BaseEntity;
import com.starlink.common.enums.AcquireModeEnum;
import com.starlink.common.enums.ActionTypeEum;
import com.starlink.common.enums.OrderTypeEnum;
import com.starlink.common.strategy.IPayNotify;
import com.starlink.common.utils.AssertUtil;
import com.starlink.mapper.OtherOrderMapper;
import com.starlink.model.domain.OtherOrderDO;
import com.starlink.model.request.PointsAcquireBillRequest;
import com.starlink.model.request.PointsUseBillRequest;
import com.starlink.model.request.mall.WechatPay;
import com.starlink.model.request.mall.WechatRefund;
import com.starlink.model.vo.OtherOrderVO;
import com.starlink.service.OtherOrderService;
import com.starlink.service.PointsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

import static com.starlink.common.enums.AcquireModeEnum.TUI_KUAN;
import static com.starlink.common.enums.AcquireModeEnum.XIAO_FEI;
import static com.starlink.common.enums.ActionTypeEum.INTEGRATION_CANCEL;
import static com.starlink.common.enums.ActionTypeEum.PLACE_ORDER_AND_CONSUME;

/**
 * @author zhanghong
 * @version StarPayNotify.java, v 0.1 2024/4/26 00:29 下午 StarPayNotify Exp $$
 */
@Component
public class StarPayNotify implements IPayNotify {

    @Autowired
    private OtherOrderService otherOrderService;
    @Autowired
    private OtherOrderMapper otherOrderMapper;
    @Autowired
    private PointsService pointsService;

    @Override
    public boolean supports(Integer orderType) {
        return orderType.equals(OrderTypeEnum.STAR.getType());
    }

    @Override
    public void handle(WechatPay wechatPay) {
        OtherOrderVO orderDO = otherOrderService.getByKey(Long.parseLong(wechatPay.getOrderId()));
        AssertUtil.assertNotNull(orderDO, "订单不存在");
        AssertUtil.assertTrue(orderDO.getOrderStatus() == 1, "订单状态不是待支付");

        // 更新订单数据和状态
        orderDO.setWxPayOrderNo(wechatPay.getWeChatOrderId());
        orderDO.setOrderStatus(2);
        otherOrderService.lambdaUpdate()
                .set(OtherOrderDO::getWxPayOrderNo, wechatPay.getWeChatOrderId())
                .set(OtherOrderDO::getOrderStatus, 2)
                .eq(BaseEntity::getId, orderDO.getId())
                .update();

        // 增加积分
        PointsAcquireBillRequest billRequest = new PointsAcquireBillRequest();
        billRequest.setUserId(orderDO.getUserId());
        billRequest.setTitle(PLACE_ORDER_AND_CONSUME.getType());
        billRequest.setAcquireMode(XIAO_FEI.getType());
        billRequest.setAcquirePoint(new BigDecimal(orderDO.getOrderMoney()).intValue());
        pointsService.addPoint(billRequest);
    }

    @Override
    public void refundHandle(WechatRefund wechatPay) {
        OtherOrderDO orderDO = otherOrderMapper.selectDOById(wechatPay.getOrderId());
        if (orderDO == null) {
            return;
        }
        orderDO.setOrderStatus(3);
        orderDO.setUpdateTime(new Date());
        orderDO.setWxRefundOrderNo(wechatPay.getWxRefundNum());
        orderDO.setRefundMoney(wechatPay.getBalance());
        otherOrderService.saveOrUpdate(orderDO);

        // 扣减积分
        if (wechatPay.getRefund() == 1) {
            wechatPay.setRefund(NumberUtil.mul(orderDO.getOrderMoney(), 100).intValue());
        }
        PointsUseBillRequest billRequest = new PointsUseBillRequest();
        billRequest.setUserId(orderDO.getUserId());
        billRequest.setTitle(ActionTypeEum.CONSUMPTION_REFUND.getType());
        billRequest.setConsumeMode(AcquireModeEnum.TUI_KUAN.getType());
        billRequest.setConsumePonts(NumberUtil.div(String.valueOf(wechatPay.getRefund()), "100", 2).intValue());
        pointsService.consumPoint(billRequest);
    }
}
